set more off
graph drop _all

use Data/Estimation_sample.dta, clear
	
	gen nexis_buildings  = ROOFME/PROOFME
		
		
	gen roofratio = ROOFME/ROOFTI
	gen roofratiobuildings = roofratio*nexis_buildings
		
	gen roofrelativeIV = (roofratio*nexis_buildings/[moduleprice])/1000

	merge 1:1 postcode contract_quarter using Data/postcodequarter_paymentconcession.dta 
	
	keep if _merge ==3
	
* 3 quarters is nine months, total of 28 quarters in sample
local length = 3
	 
forval t = 0(1)`length' {
	gen event_after`t' =  SHCP_end ==`t' |  PFIT_starttime==`t' | PFIT_endtime==`t' | ///
	 SRES1_end ==`t' | SRES2_end==`t' | SRES_TFIT_endtime==`t' | FFIT_endtime==`t'
	 
	 
	 gen event_before`t' =  SHCP_end ==-`t'  | PFIT_starttime==-`t' | PFIT_endtime==-`t' | ///
	 SRES1_end ==-`t' | SRES2_end==-`t' | SRES_TFIT_endtime==-`t' | FFIT_endtime==-`t'
	
	gen event_after_new`t' = event_after`t'*cum
	gen event_before_new`t' = event_before`t'*cum
	
	gen event_after_high`t' = event_after`t'*roofrelativeIV
	gen event_before_high`t' = event_before`t'*roofrelativeIV
	
	drop event_after`t'
	drop event_before`t'
	
	
	
	}
		
	




label variable event_before_new3  "Event period -3"
label variable event_before_new2  "Event period -2"
label variable event_before_new1  "Event period -1"
label variable event_before_new0  "Event period "
label variable event_after_new1   "Event period +1"
label variable event_after_new2   "Event period +2"
label variable event_after_new3   "Event period +3"






xi: xtivreg2 not_concession (event_before_new3 event_before_new2 event_before_new1  event_before_new0 event_after_new1  event_after_new2 event_after_new3  ///
	= event_after_high* event_before_high* ) i.quarter  [aweight =NCustomer]   ,  fe cluster(postcode)

 outreg2 using Analysis/ES_concession, bdec(2) se  nocons label  ctitle(" ")   ///
	keep(event* cum mean_weighted TotalHouse_interp quarter ) addtext(Postcode FE, Yes) tex(fragment pretty) nonotes replace


eststo model

	coefplot (model, keep(event_before_new3  event_before_new2  event_before_new1) mcolor(gs0) ciopts(lcolor(gs0)) msymbol(o)) ///
	 (model, keep(event_before_new0 ) mcolor(gs0) ciopts(lcolor(gso)) msymbol(o)) ///
	(model, keep( event_after_new1 event_after_new2 event_after_new3 ) mcolor(gs0) ciopts(lcolor(gs0)) msymbol(o)), vertical ///
	 relocate(event_before_new3 = -2.84 event_before_new2 = -1.84 event_before_new1 = -0.84 event_before_new0 = -0 ///
	event_after_new1 =0.84  event_after_new2=1.84 event_after_new3=2.84 )   legend(off) yline(0) ///
	xtitle("Event time", size(large)) xlabel(-3(1)3, labsize(large))  xline(0, lcolor(gs10) lpattern(dash) lwidth(vthin)) yscale(range(-0.025 0.025)) ylabel(-0.02(0.01)0.02, labsize(large)) 



graph export Analysis/ES_concessionIV.png, replace
graph export Analysis/ES_concessionIV.eps, replace







xi: xtivreg2 not_direct (event_before_new3 event_before_new2 event_before_new1  event_before_new0 event_after_new1  event_after_new2 event_after_new3  ///
	= event_after_high* event_before_high* )  i.quarter  [aweight =NCustomer]   ,  fe cluster(postcode)


 outreg2 using Analysis/ES_debit, bdec(2) se  nocons label  ctitle(" ")   ///
	keep(event* cum mean_weighted TotalHouse_interp quarter ) addtext(Postcode FE, Yes) tex(fragment pretty) nonotes replace

	eststo model1

	coefplot (model1, keep(event_before_new3  event_before_new2  event_before_new1) mcolor(gs0) ciopts(lcolor(gs0)) msymbol(o)) ///
	 (model1, keep(event_before_new0 ) mcolor(gs0) ciopts(lcolor(gso)) msymbol(o)) ///
	(model1, keep( event_after_new1 event_after_new2 event_after_new3 ) mcolor(gs0) ciopts(lcolor(gs0)) msymbol(o)), vertical ///
	 relocate(event_before_new3 = -2.84 event_before_new2 = -1.84 event_before_new1 = -0.84 event_before_new0 = -0 ///
	event_after_new1 =0.84  event_after_new2=1.84 event_after_new3=2.84 )   legend(off) yline(0) ///
		xtitle("Event time", size(large)) xlabel(-3(1)3, labsize(large))  xline(0, lcolor(gs10) lpattern(dash) lwidth(vthin)) yscale(range(-0.025 0.025)) ylabel(-0.02(0.01)0.02, labsize(large)) 


graph export Analysis/ES_debitIV.png, replace
graph export Analysis/ES_debitIV.eps, replace

*/
	


xi: xtivreg2 price_green (event_before_new3 event_before_new2 event_before_new1  event_before_new0 event_after_new1  event_after_new2 event_after_new3  ///
	= event_after_high* event_before_high* )  i.quarter  [aweight =NCustomer]   ,  fe cluster(postcode)




eststo model2

	coefplot (model2, keep(event_before_new3  event_before_new2  event_before_new1) mcolor(gs0) ciopts(lcolor(gs0)) msymbol(o)) ///
	 (model2, keep(event_before_new0 ) mcolor(gs0) ciopts(lcolor(gso)) msymbol(o)) ///
	(model2, keep( event_after_new1 event_after_new2 event_after_new3 ) mcolor(gs0) ciopts(lcolor(gs0)) msymbol(o)), vertical ///
	 relocate(event_before_new3 = -2.84 event_before_new2 = -1.84 event_before_new1 = -0.84 event_before_new0 = -0 ///
	event_after_new1 =0.84  event_after_new2=1.84 event_after_new3=2.84 )   legend(off) yline(0) ///
	xtitle("Event time", size(large)) xlabel(-3(1)3, labsize(large))  xline(0, lcolor(gs10) lpattern(dash) lwidth(vthin)) yscale(range(-0.025 0.025)) ylabel(-0.02(0.01)0.02, labsize(large)) 

	graph export Analysis/ESpricegreenIV.png, replace

	graph export Analysis/ESpricegreenIV.eps, replace



